<?php
    include_once('../src/classes/class.mysql.php');
    
    session_start();
    
    $comtec_id_machine = $_GET["id_machine"];
    
    $db = new MySQL();
    
    $tsql = "SELECT comtec_id_order,cod_order,o.operation_type,m.cod_mainteiner,m.brand,
		    m.model,ow.company_name as Propietario,
	            CONCAT_WS(' ',r.name,r.lastname1,r.lastname2) as Encargado,
	            DATE_FORMAT((SELECT op.operation_date_end
                                      FROM operation as op
                                      WHERE op.comtec_id_machine = m.comtec_id_machine AND status_reg = 1
                                      ORDER BY op.operation_date_end DESC Limit 1)
                    ,'%d/%m/%Y') AS last_operation,
                    DATE_FORMAT(o.order_start_date,'%d/%m/%Y') as Fecha,
	            (SELECT COUNT(*)
                     FROM orders as o1 INNER JOIN operation as op1 ON o1.comtec_id_order = op1.comtec_id_order
		     WHERE o1.comtec_id_order = o.comtec_id_order AND o1.status_reg=1 AND op1.status_reg=1) as cant 
             FROM (((orders as o INNER JOIN machines as m ON o.comtec_id_machine = m.comtec_id_machine)
                  INNER JOIN responsible as r ON r.comtec_id_responsible = o.comtec_id_responsible)
                  INNER JOIN owner as ow ON m.comtec_id_owner = ow.comtec_id_owner)
             WHERE m.comtec_id_machine = $comtec_id_machine AND o.status_reg=1
             ORDER BY o.order_start_date DESC
    ";
    
    $stmt = $db->query($tsql);       
    
    $texto = "<table class='table'>
               <tr>";
    $texto .= " <th colspan=3>";
    $texto .= " <th>C&oacute;digo</th>
                <th>Tipo</th>
                <th>C&oacute;d. m&aacute;quina</th>
                <th>Marca</th>
                <th>Modelo</th>               
                <th>Propietario</th>
                <th>Encargado</th>";
    $texto .= " <th>Fecha &uacute;lt. oper.</th>
                <th>Fecha a operar</th>";  
    $texto .= " <th>Realizada</th>"; 
    $texto .= "</tr>";  
    
    $cont = 0;   
    while ($row = mysql_fetch_array($stmt, MYSQL_ASSOC)){
     $cont += 1;    
     $comtec_id_order=$row['comtec_id_order'];
     $Codigo=$row['cod_order'];   
     $Tipo=$row['operation_type'];
     $Cod_maquina=$row['cod_mainteiner'];
     $Marca=$row['brand'];
     $Modelo=$row['model'];  
     $Propietario=$row['Propietario'];
     $Encargado=$row['Encargado'];
     $Fecha_ult=$row['last_operation'];
     $Fecha=$row['Fecha'];
     $Cant=$row['cant'];
   
     $texto .= "<tr>";
     
     if($Cant > 0){
      $url_view='http://'.$_SERVER['HTTP_HOST'].'/iconos/viewOperation.png';
      $texto .= "<td><a href='javascript:viewOperation(".$comtec_id_order.");'><img src='$url_view' alt='ver' title='Ver operacion' style='border: 0px;' /></a></td>";
     }else{
      $url_add='http://'.$_SERVER['HTTP_HOST'].'/iconos/addOperation.png';
      $texto .= "<td><a href='javascript:loadAddOperation(".$comtec_id_order.");'><img src='$url_add' alt='agregar' title='Agregar operacion' style='border: 0px;' /></a></td>";
     }
     $url_edit='http://'.$_SERVER['HTTP_HOST'].'/iconos/edit.png';
     $texto .= "<td><a href='javascript:loadOrder(".$comtec_id_order.");'><img src='$url_edit' alt='editar' title='Editar orden de trabajo' style='border: 0px;' /></a></td>";
     
     $url_delete='http://'.$_SERVER['HTTP_HOST'].'/iconos/delete.png';
     $texto .= "<td><a href='javascript:deleteOrder(".$comtec_id_order.");'><img src='$url_delete' alt='eliminar' title='Eliminar orden de trabajo' style='border: 0px;' /></a></td>";
     
     $texto .= "<td><a href='javascript:order(".$comtec_id_order.");'>$Codigo</a></td>";
     switch ($Tipo) {
      case 1: 
             $Tipo = "Mantenci&oacute;n";
      break;
      case 2: 
             $Tipo = "Emergencia";
      break; 
      case 3: 
             $Tipo = "Instalaci&oacute;n";
      break;
      case 4: 
             $Tipo = "Reparaci&oacute;n";
      break;
      case 0: 
             $Tipo = "";
      break;
     }
     $texto .= "<td>$Tipo</td>";
     $texto .= "<td>$Cod_maquina</td>";
     $texto .= "<td>$Marca</td>";
     $texto .= "<td>$Modelo</td>";
     $texto .= "<td>$Propietario</td>";
     $texto .= "<td>$Encargado</td>";
     $texto .= "<td>$Fecha_ult</td>";
     $texto .= "<td>$Fecha</td>";
     if($Cant == 0){
      $realizada = "No";   
     }else{$realizada = "Si";}
     $texto .= "<td>$realizada</td>";
     $texto .= "</tr>";
    }
    $texto .= "</table>";
    $texto .= '<div id="result"></div>';
    echo "<p class='total'>Total: $cont</p>".$texto;
?>
